home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / WIN32PDHUTIL.PYC (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  1999-07-21  |  5.7 KB  |  138 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. '''Utilities for the win32 Performance Data Helper module
  5.  
  6. Example:
  7.   To get a single bit of data:
  8.   >>> import win32pdhutil
  9.   >>> win32pdhutil.GetPerformanceAttributes("Memory", "Available Bytes")
  10.   6053888
  11.   >>> win32pdhutil.FindPerformanceAttributesByName("python", counter="Virtual Bytes")
  12.   [22278144]
  13.   
  14.   First example returns data which is not associated with any specific instance.
  15.   
  16.   The second example reads data for a specific instance - hence the list return - 
  17.   it would return one result for each instance of Python running.
  18.  
  19.   In general, it can be tricky finding exactly the "name" of the data you wish to query.  
  20.   Although you can use <om win32pdh.EnumObjectItems>(None,None,(eg)"Memory", -1) to do this, 
  21.   the easiest way is often to simply use PerfMon to find out the names.
  22. '''
  23. import win32pdh
  24. import string
  25. import win32api
  26. error = win32pdh.error
  27.  
  28. def GetPerformanceAttributes(object, counter, instance = None, inum = -1, format = win32pdh.PDH_FMT_LONG, machine = None):
  29.     path = win32pdh.MakeCounterPath((machine, object, instance, None, inum, counter))
  30.     hq = win32pdh.OpenQuery()
  31.     
  32.     try:
  33.         hc = win32pdh.AddCounter(hq, path)
  34.         
  35.         try:
  36.             win32pdh.CollectQueryData(hq)
  37.             (type, val) = win32pdh.GetFormattedCounterValue(hc, format)
  38.             return val
  39.         finally:
  40.             win32pdh.RemoveCounter(hc)
  41.  
  42.     finally:
  43.         win32pdh.CloseQuery(hq)
  44.  
  45.  
  46.  
  47. def FindPerformanceAttributesByName(instanceName, object = 'Process', counter = 'ID Process', format = win32pdh.PDH_FMT_LONG, machine = None):
  48.     '''Find peformance attributes by instance name.
  49. \t
  50. \tGiven a process name, return a list with the requested attributes.
  51. \tMost useful for returning a tuple of PIDs given a process name.
  52. \t'''
  53.     (items, instances) = win32pdh.EnumObjectItems(None, None, object, -1)
  54.     instance_dict = { }
  55.     for instance in instances:
  56.         
  57.         try:
  58.             instance_dict[instance] = instance_dict[instance] + 1
  59.         except KeyError:
  60.             0
  61.             0
  62.             instances
  63.             instance_dict[instance] = 0
  64.         except:
  65.             0
  66.  
  67.     
  68.     ret = []
  69.     for instance, max_instances in instance_dict.items():
  70.         for inum in xrange(max_instances + 1):
  71.             pass
  72.         
  73.     
  74.     return ret
  75.  
  76.  
  77. def ShowAllProcesses():
  78.     object = 'Process'
  79.     (items, instances) = win32pdh.EnumObjectItems(None, None, object, win32pdh.PERF_DETAIL_WIZARD)
  80.     instance_dict = { }
  81.     for instance in instances:
  82.         
  83.         try:
  84.             instance_dict[instance] = instance_dict[instance] + 1
  85.         except KeyError:
  86.             0
  87.             0
  88.             instances
  89.             instance_dict[instance] = 0
  90.         except:
  91.             0
  92.  
  93.     
  94.     items = [
  95.         'ID Process'] + items[:5]
  96.     print 'Process Name', string.join(items, ',')
  97.     for instance, max_instances in instance_dict.items():
  98.         for inum in xrange(max_instances + 1):
  99.             hq = win32pdh.OpenQuery()
  100.             hcs = []
  101.             for item in items:
  102.                 path = win32pdh.MakeCounterPath((None, object, instance, None, inum, item))
  103.                 hcs.append(win32pdh.AddCounter(hq, path))
  104.             
  105.             win32pdh.CollectQueryData(hq)
  106.             print '%-15s\t' % instance[:15],
  107.             for hc in hcs:
  108.                 (type, val) = win32pdh.GetFormattedCounterValue(hc, win32pdh.PDH_FMT_LONG)
  109.                 print '%5d' % val,
  110.                 win32pdh.RemoveCounter(hc)
  111.             
  112.             print 
  113.             win32pdh.CloseQuery(hq)
  114.         
  115.     
  116.  
  117.  
  118. def BrowseCallBack(counter):
  119.     (machine, object, instance, parentInstance, index, counterName) = win32pdh.ParseCounterPath(counter)
  120.     result = GetPerformanceAttributes(object, counterName, instance, index, win32pdh.PDH_FMT_DOUBLE, machine)
  121.     print "Value of '%s' is" % counter, result
  122.     print "Added '%s' on object '%s' (machine %s), instance %s(%d)-parent of %s" % (counterName, object, machine, instance, index, parentInstance)
  123.  
  124.  
  125. def browse():
  126.     print 'Browsing for counters...'
  127.     win32pdh.BrowseCounters(None, 0, BrowseCallBack, win32pdh.PERF_DETAIL_WIZARD, 'Python Browser')
  128.  
  129.  
  130. def test():
  131.     print 'Virtual Bytes = ', FindPerformanceAttributesByName('python', counter = 'Virtual Bytes')
  132.     print 'Available Bytes = ', GetPerformanceAttributes('Memory', 'Available Bytes')
  133.     print win32pdh.EnumObjectItems(None, None, 'Memory', -1)
  134.  
  135. if __name__ == '__main__':
  136.     browse()
  137.  
  138.